﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QuickExcel
{
    public partial class CurrencyConverter : Form
    {
        object[,] oLatestRates;

        public CurrencyConverter()
        {
            InitializeComponent();
        }

        private void CurrencyConverter_Load(object sender, EventArgs e)
        {
            UpdateRates();
            if (oLatestRates == null)
            {
                this.Close();
                return;
            }

            txtSource.Text = Globals.ThisAddIn.Application.Selection.Address;
            txtResult.Text = txtSource.Text;

            lstFrom.Items.Clear();
            lstTo.Items.Clear();            

            for (int i = 1; i <= oLatestRates.GetLength(0); i++)
            {
                lstFrom.Items.Add(oLatestRates[i, 1]);
                lstTo.Items.Add(oLatestRates[i, 1]);
            }
        }

        private void txtSource_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Range rng;
            rng = Globals.ThisAddIn.Application.InputBox("Please select range", "Select Range", txtSource.Text, this.Left - 5, this.Top + 5, Type: 8);
            rng.Select();
            txtSource.Text = rng.Address;
            this.Activate();
        }

        private void txtResult_Click(object sender, EventArgs e)
        {
            Microsoft.Office.Interop.Excel.Range rng;
            rng = Globals.ThisAddIn.Application.InputBox("Please select range", "Select Range", txtResult.Text, this.Left - 5, this.Top + 5, Type: 8);
            rng.Select();
            txtResult.Text = rng.Address;
            this.Activate();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string sReslutRange;
            object[,] arrSource;

            if (Globals.ThisAddIn.Application.ActiveSheet.Range(txtSource.Text).Cells.Count > 1)
            {
                arrSource = Globals.ThisAddIn.Application.ActiveSheet.Range(txtSource.Text).Value2;
            }
            else if ((Globals.ThisAddIn.Application.ActiveSheet.Range(txtSource.Text).Text).Length > 0)
            {
                Int32[] lowerBounds = { 1, 1 };
                Int32[] lengths = { 1, 1 };
                arrSource = (object[,])Array.CreateInstance(typeof(object), lengths, lowerBounds);
                arrSource[1, 1] = Globals.ThisAddIn.Application.ActiveSheet.Range(txtSource.Text).value2;
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Source range is empty. Please select some numeric values");
                this.Activate();
                return;
            }
                        
            if (chkOverwrite.Checked)
                sReslutRange = txtSource.Text;
            else
                sReslutRange = txtResult.Text;

            if (txtSource.Text.Length == 0)
                MessageBox.Show("Please enter a range in 'Source Range'");
            else if (sReslutRange.Length == 0)
                MessageBox.Show("Please enter a range in 'Result Range'");
            else if (lstFrom.Text.Length == 0 || lstTo.Text.Length == 0)
                MessageBox.Show("Please select both 'From' currency and 'To' currency.");
            else if (lstFrom.Text == lstTo.Text)
                MessageBox.Show("'From' currency and 'To' currency are same.");
            else
            {
                Currency.ConvertCurrency(lstFrom.Text, arrSource, lstTo.Text,oLatestRates);
                Globals.ThisAddIn.Application.ActiveSheet.Range(sReslutRange).Value2 = arrSource;
                this.Close();
            }       
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UpdateRates()
        {
            oLatestRates = Currency.GetLatestRates();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            UpdateRates();

            if (oLatestRates == null)
            {
                this.Close();
                return;
            }

            lstFrom.Items.Clear();
            lstTo.Items.Clear();

            for (int i = 1; i <= oLatestRates.GetLength(0); i++)
            {
                lstFrom.Items.Add(oLatestRates[i, 1]);
                lstTo.Items.Add(oLatestRates[i, 1]);
            }
        }
    }
}
